package interfaces;


import java.awt.Dimension;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;

import javax.swing.Box;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.SwingConstants;
import java.awt.ComponentOrientation;
import javax.swing.JComboBox;


import entidades.Titular;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class EmitirLicencia extends PanelGeneral {

	private static final long serialVersionUID = 1L;
	private JTextField txtTipoDeDocumento;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private BusquedaDeTitularDialogo idBusquedaTitular;
	public EmitirLicencia emitirlicencia;
	public JComboBox<String> comboBox;
	public JButton btnEmitirLicencia;
	public JPanel panel_3;
	public JLabel lblNewLabel;
	public JLabel lblNmeroDeDocumento;
	public JLabel lblNombreYApellido;
	public JLabel lblFechaDeNacimiento;
	public JLabel lblDireccion;
	public JLabel lblGrupoSanguineo;
	public JLabel lblFactor;
	public JLabel lblClase;
	public JLabel lblDonanteDeOrganos;
	public PantallaPrincipal p;
	public Titular t;

	public EmitirLicencia(PantallaPrincipal framePerteneciente) {
		
		super(framePerteneciente);
		emitirlicencia=this;
		p= framePerteneciente;
		setMinimumSize(new Dimension(500, 600));
		setPreferredSize(new Dimension(800, 550));
		setAlignmentY(1.0f);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		setNombreCU("Emitir licencia");
		
		JPanel panelUsuario = new JPanel();
		add(panelUsuario);
		panelUsuario.setMaximumSize(panelLabel);
		
		JLabel lblNombreDelUsuario = new JLabel("#NombredelUsuario");
		panelUsuario.add(lblNombreDelUsuario);
		
		JPanel panelTrabajo = new JPanel();
		panelTrabajo.setMinimumSize(new Dimension(600, 500));
		panelTrabajo.setPreferredSize(new Dimension(600, 500));
		add(panelTrabajo);
		panelTrabajo.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	
		panelTrabajo.setLayout(new BoxLayout(panelTrabajo, BoxLayout.Y_AXIS));
		this.add(panelTrabajo, BorderLayout.CENTER);
		panelTrabajo.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setPreferredSize(new Dimension(600, 500));
		panelTrabajo.add(panel);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		horizontalStrut_1.setMaximumSize(new Dimension(500, 15));
		horizontalStrut_1.setMinimumSize(new Dimension(200, 15));
		horizontalStrut_1.setPreferredSize(new Dimension(200, 15));
		panel.add(horizontalStrut_1);
		
		JPanel panel_5 = new JPanel();
		panel_5.setMaximumSize(new Dimension(32767, 90));
		panel_5.setLayout(new BoxLayout(panel_5, BoxLayout.Y_AXIS));
		panel_5.setMinimumSize(new Dimension(500, 20));
		panel_5.setPreferredSize(new Dimension(500, 30));
		panel.add(panel_5);
		panel_5.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel panel_7 = new JPanel();
		panel_5.add(panel_7);
		
		JLabel lblElegUnTitular = new JLabel("Eleg\u00ED un titular para poder emitir una licencia");
		panel_7.add(lblElegUnTitular);
		
		JPanel panel_6 = new JPanel();
		panel_6.setPreferredSize(new Dimension(600, 35));
		panel_5.add(panel_6);
		
		JButton btnNewButton = new JButton("B\u00FAsqueda de titular");
		btnNewButton.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode()==KeyEvent.VK_ENTER){
					
					if(idBusquedaTitular==null){	idBusquedaTitular= new BusquedaDeTitularDialogo(getFramePerteneciente(),true,emitirlicencia);}
					idBusquedaTitular.activarDG();
				}
				}
		});
		btnNewButton.setPreferredSize(new Dimension(200, 28));
		panel_6.add(btnNewButton);
		btnNewButton.addActionListener(new busquedatitularaction());
		
		JPanel panel_1 = new JPanel();
		panel_1.setMaximumSize(new Dimension(800, 350));
		panel_1.setPreferredSize(new Dimension(800, 350));
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		panel_1.setMinimumSize(new Dimension(800, 300));
		panel.add(panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel panel_3 = new JPanel();
		panel_3.setMinimumSize(new Dimension(300, 300));
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));
		panel_3.setPreferredSize(new Dimension(170, 350));
		panel_1.add(panel_3);
		
		Component horizontalStrut = Box.createHorizontalStrut(20);
		horizontalStrut.setMaximumSize(new Dimension(150, 5));
		horizontalStrut.setPreferredSize(new Dimension(20, 10));
		panel_3.add(horizontalStrut);
		
		lblNewLabel = new JLabel("TIPO DE DOCUMENTO");
		lblNewLabel.setForeground(Color.BLACK);
		lblNewLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblNewLabel);
		
		Component horizontalStrut_2 = Box.createHorizontalStrut(20);
		horizontalStrut_2.setMinimumSize(new Dimension(40, 15));
		horizontalStrut_2.setPreferredSize(new Dimension(40, 20));
		horizontalStrut_2.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_2);
		
		lblNmeroDeDocumento = new JLabel("N\u00DAMERO DE DOCUMENTO");
		lblNmeroDeDocumento.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblNmeroDeDocumento);
		
		Component horizontalStrut_3 = Box.createHorizontalStrut(20);
		horizontalStrut_3.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_3.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_3);
		
		lblNombreYApellido = new JLabel("NOMBRE Y APELLIDO");
		lblNombreYApellido.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblNombreYApellido);
		
		Component horizontalStrut_7 = Box.createHorizontalStrut(20);
		horizontalStrut_7.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_7.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_7);
		
		lblFechaDeNacimiento = new JLabel("FECHA DE NACIMIENTO");
		lblFechaDeNacimiento.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblFechaDeNacimiento);
		
		Component horizontalStrut_8 = Box.createHorizontalStrut(20);
		horizontalStrut_8.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_8.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_8);
		
		lblDireccion = new JLabel("DIRECCION");
		lblDireccion.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblDireccion);
		
		Component horizontalStrut_10 = Box.createHorizontalStrut(20);
		horizontalStrut_10.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_10.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_10);
		
		lblGrupoSanguineo = new JLabel("GRUPO SANGUINEO");
		lblGrupoSanguineo.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblGrupoSanguineo);
		
		Component horizontalStrut_12 = Box.createHorizontalStrut(20);
		horizontalStrut_12.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_12.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_12);
		
		lblFactor = new JLabel("FACTOR");
		lblFactor.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblFactor);
		
		Component horizontalStrut_14 = Box.createHorizontalStrut(20);
		horizontalStrut_14.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_14.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_14);
		
		lblDonanteDeOrganos = new JLabel("DONANTE DE ORGANOS");
		lblDonanteDeOrganos.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblDonanteDeOrganos);
		
		Component horizontalStrut_16 = Box.createHorizontalStrut(20);
		horizontalStrut_16.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_16.setMaximumSize(new Dimension(150, 22));
		panel_3.add(horizontalStrut_16);
		
		lblClase = new JLabel("CLASE");
		lblClase.setFont(new Font("SansSerif", Font.BOLD, 12));
		panel_3.add(lblClase);
		
		
		JPanel panel_4 = new JPanel();
		panel_4.setMaximumSize(new Dimension(300, 300));
		panel_4.setMinimumSize(new Dimension(300, 400));
		panel_4.setPreferredSize(new Dimension(300, 350));
		panel_1.add(panel_4);
		panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.Y_AXIS));
		
		txtTipoDeDocumento = new JTextField();
		txtTipoDeDocumento.setSelectedTextColor(Color.GRAY);
		txtTipoDeDocumento.setSelectionColor(Color.GRAY);
		txtTipoDeDocumento.setSize(new Dimension(100, 0));
		txtTipoDeDocumento.setFont(new Font("Verdana", Font.PLAIN, 15));
		txtTipoDeDocumento.setMaximumSize(new Dimension(200, 28));
		txtTipoDeDocumento.setMinimumSize(new Dimension(200, 28));
		txtTipoDeDocumento.setPreferredSize(new Dimension(28, 30));
		panel_4.add(txtTipoDeDocumento);
		txtTipoDeDocumento.setColumns(10);
		
		
		
		Component horizontalStrut_4 = Box.createHorizontalStrut(20);
		horizontalStrut_4.setMinimumSize(new Dimension(20, 30));
		horizontalStrut_4.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_4.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_4);
		
		textField_1 = new JTextField();
		textField_1.setSelectedTextColor(Color.LIGHT_GRAY);
		textField_1.setForeground(Color.LIGHT_GRAY);
		textField_1.setDisabledTextColor(new Color(142, 143, 145));
		textField_1.setSelectionColor(Color.GRAY);
		textField_1.setSize(new Dimension(100, 0));
		textField_1.setPreferredSize(new Dimension(200, 28));
		textField_1.setMinimumSize(new Dimension(200, 28));
		textField_1.setMaximumSize(new Dimension(200, 28));
		textField_1.setHorizontalAlignment(SwingConstants.LEFT);
		textField_1.setFont(new Font("Verdana", Font.PLAIN, 15));
		textField_1.setColumns(10);
		panel_4.add(textField_1);
		
		Component horizontalStrut_5 = Box.createHorizontalStrut(20);
		horizontalStrut_5.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_5.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_5);
		
		textField_2 = new JTextField();
		textField_2.setSize(new Dimension(100, 0));
		textField_2.setPreferredSize(new Dimension(70, 28));
		textField_2.setMinimumSize(new Dimension(70, 28));
		textField_2.setMaximumSize(new Dimension(200, 28));
		textField_2.setHorizontalAlignment(SwingConstants.LEFT);
		textField_2.setFont(new Font("Verdana", Font.PLAIN, 15));
		textField_2.setColumns(10);
		panel_4.add(textField_2);
		
		Component horizontalStrut_6 = Box.createHorizontalStrut(20);
		horizontalStrut_6.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_6.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_6);
		
		textField_3 = new JTextField();
		textField_3.setSize(new Dimension(100, 0));
		textField_3.setPreferredSize(new Dimension(70, 28));
		textField_3.setMinimumSize(new Dimension(70, 28));
		textField_3.setMaximumSize(new Dimension(200, 28));
		textField_3.setHorizontalAlignment(SwingConstants.LEFT);
		textField_3.setFont(new Font("Verdana", Font.PLAIN, 15));
		textField_3.setColumns(10);
		panel_4.add(textField_3);
		
		Component horizontalStrut_9 = Box.createHorizontalStrut(20);
		horizontalStrut_9.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_9.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_9);
		
		textField_4 = new JTextField();
		textField_4.setSize(new Dimension(100, 0));
		textField_4.setPreferredSize(new Dimension(70, 28));
		textField_4.setMinimumSize(new Dimension(70, 28));
		textField_4.setMaximumSize(new Dimension(200, 28));
		textField_4.setHorizontalAlignment(SwingConstants.LEFT);
		textField_4.setFont(new Font("Verdana", Font.PLAIN, 15));
		textField_4.setColumns(10);
		panel_4.add(textField_4);
		
		Component horizontalStrut_11 = Box.createHorizontalStrut(20);
		horizontalStrut_11.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_11.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_11);
		
		textField_5 = new JTextField();
		textField_5.setSize(new Dimension(100, 0));
		textField_5.setPreferredSize(new Dimension(70, 28));
		textField_5.setMinimumSize(new Dimension(70, 28));
		textField_5.setMaximumSize(new Dimension(200, 28));
		textField_5.setHorizontalAlignment(SwingConstants.LEFT);
		textField_5.setFont(new Font("Verdana", Font.PLAIN, 15));
		textField_5.setColumns(10);
		panel_4.add(textField_5);
		
		Component horizontalStrut_13 = Box.createHorizontalStrut(20);
		horizontalStrut_13.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_13.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_13);
		
		textField_6 = new JTextField();
		textField_6.setSize(new Dimension(100, 0));
		textField_6.setPreferredSize(new Dimension(70, 28));
		textField_6.setMinimumSize(new Dimension(70, 28));
		textField_6.setMaximumSize(new Dimension(200, 28));
		textField_6.setHorizontalAlignment(SwingConstants.LEFT);
		textField_6.setFont(new Font("Verdana", Font.PLAIN, 15));
		textField_6.setColumns(10);
		panel_4.add(textField_6);
		
		Component horizontalStrut_15 = Box.createHorizontalStrut(20);
		horizontalStrut_15.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_15.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_15);
		
		textField_7 = new JTextField();
		textField_7.setSize(new Dimension(100, 0));
		textField_7.setPreferredSize(new Dimension(70, 28));
		textField_7.setMinimumSize(new Dimension(70, 28));
		textField_7.setMaximumSize(new Dimension(200, 28));
		textField_7.setHorizontalAlignment(SwingConstants.LEFT);
		textField_7.setFont(new Font("Verdana", Font.PLAIN, 15));
		textField_7.setColumns(10);
		panel_4.add(textField_7);
		
		Component horizontalStrut_17 = Box.createHorizontalStrut(20);
		horizontalStrut_17.setPreferredSize(new Dimension(30, 10));
		horizontalStrut_17.setMaximumSize(new Dimension(300, 10));
		panel_4.add(horizontalStrut_17);
		
		comboBox = new JComboBox<String>();
		comboBox.addItem("A");
		comboBox.addItem("B");
		comboBox.addItem("C");
		comboBox.addItem("D");
		comboBox.addItem("E");
		comboBox.addItem("F");
		this.comboBox.setSelectedItem(null);
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (comboBox.getSelectedItem()!= null)
					activaremitirlicencia();
			}
		});
		comboBox.setMaximumSize(new Dimension(200, 26));
		panel_4.add(comboBox);
		
		JPanel panel_2 = new JPanel();
		panel_2.setMaximumSize(new Dimension(32767, 60));
		panel.add(panel_2);
		
		btnEmitirLicencia = new JButton("Emitir licencia");
		btnEmitirLicencia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				p.cambiarCU(new ConfirmarEmitirLicencia(p,t));
			}
		});
		btnEmitirLicencia.setPreferredSize(new Dimension(110, 32));
		btnEmitirLicencia.setMinimumSize(new Dimension(105, 32));
		btnEmitirLicencia.setMaximumSize(new Dimension(110, 32));
		panel_2.add(btnEmitirLicencia);
		btnEmitirLicencia.setEnabled(false);
		
		
		/*
		habilitarControles(txtTipoDeDocumento,false);
		habilitarControles(textField_1,false);
		habilitarControles(textField_2,false);
		habilitarControles(textField_3,false);
		habilitarControles(textField_4,false);
		habilitarControles(textField_5,false);
		habilitarControles(textField_6,false);
		habilitarControles(textField_7,false);
		comboBox.setEnabled(false);
		textField_7.setEnabled(false);
		*/
		
		habilitarControles(panel_4,false);
		habilitarColores(panel_3,false);
		
	}
	
	public class busquedatitularaction extends AbstractAction{
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent arg0) {
			if(idBusquedaTitular==null){	idBusquedaTitular= new BusquedaDeTitularDialogo(getFramePerteneciente(),true,emitirlicencia);}
			idBusquedaTitular.activarDG();
		}
	}
	
	public void activartitular(Titular titular)
	{
		t=titular;
		comboBox.setEnabled(true);
		txtTipoDeDocumento.setText(String.valueOf(titular.tipoDoc));
		textField_1.setText(String.valueOf(titular.nroDoc));
		textField_2.setText(titular.nombre +" "+ titular.apellido);
		
		java.util.Date date = new java.util.Date(); 
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd/MM/yyyy");
		String fecha = sdf.format(titular.fechaNac);
		//TODO
		textField_3.setText(fecha);
		textField_4.setText(titular.direccion);
		textField_5.setText(titular.grupoSang);
		textField_6.setText(titular.factor);
		textField_7.setText(String.valueOf(titular.donante));
		habilitarColores(panel_3,false);
		lblNewLabel.setForeground(Color.BLACK);
		lblNmeroDeDocumento.setForeground(Color.BLACK);
		lblNombreYApellido.setForeground(Color.BLACK);
		lblDireccion.setForeground(Color.BLACK);
		lblFechaDeNacimiento.setForeground(Color.BLACK);
		lblGrupoSanguineo.setForeground(Color.BLACK);
		lblFactor.setForeground(Color.BLACK);
		lblDonanteDeOrganos.setForeground(Color.BLACK);
		this.comboBox.setSelectedItem(null);
		lblClase.setForeground(Color.RED);
	}
	
	public void activaremitirlicencia()
	{
		lblClase.setForeground(Color.BLACK);
		btnEmitirLicencia.setEnabled(true);
	}
	
	 void habilitarControles(Component  component, boolean b) {
         if (component instanceof JTextField ) {
                 JTextField  text = ((JTextField ) component);
                 text.setEnabled(b);
                 if (b) {
                         text.setDisabledTextColor(new Color (0, 0, 0));
                         text.setBackground(new Color (255, 255, 255));
                 } else {
                         text.setDisabledTextColor(new Color (0, 40, 240));
                         text.setBackground(new Color (240, 240, 240));
                 }
         } else {
                 if (component instanceof Container ) {
                         for (Component  c : ((Container ) component).getComponents()) {
                                 habilitarControles(c, b);
                         }
                 }
         }
         
         if (component instanceof JComboBox ) {
             component.setEnabled(b);
     } 
 }
	 
	 void habilitarColores(Component  component, boolean b) {
         if (component instanceof JLabel ) {
                 JLabel  text = (JLabel) (component);
                 
                 if (b) {
                	 	text.setForeground(Color.BLACK);
                 		} 
                 
                 else   {
                        text.setForeground(Color.GRAY);
                 		}
         } else {
                 if (component instanceof Container ) {
                         for (Component  c : ((Container ) component).getComponents()) {
                                 habilitarColores(c, b);
                         }
                 }
         }
 }
}
